package com.pay.wx;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import common.ClientUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * 微信支付分
 *
 * @author chen
 */
public class WXPayPoints {

    /**
     * 授权签约信息
     *
     * @param service_id
     * @param appid
     * @param authorization_code
     * @param notify_url
     * @return
     */
    public String payscorePermissions(String service_id, String appid, String authorization_code, String notify_url) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service_id", service_id);
        paramMap.put("appid", appid);
        paramMap.put("authorization_code", authorization_code);
        paramMap.put("notify_url", notify_url);
        try {
            String result = ClientUtils.doPostMethod("https://api.mch.weixin.qq.com/v3/payscore/permissions", JSONObject.toJSONString(paramMap), "UTF-8");
            JSONObject jsonObject = JSONObject.parseObject(result);
            return jsonObject.getString("apply_permissions_token");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询用户授权情况
     *
     * @param service_id
     * @param authorization_code
     * @return
     */
    public JSONObject queryPermissions(String service_id, String authorization_code) {
        String url = "https://api.mch.weixin.qq.com/v3/payscore/permissions/authorization-code/" + authorization_code + "?service_id" + service_id;
        try {
            String result = ClientUtils.doGetMethod(url);
            return JSONObject.parseObject(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



}
